<?php

use console\db\Migration;

/**
 * 创建系统运行相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091355_t_site extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 应用站点信息表
         * @table `site`
         */
        $this->tableBuilder([
            'name' => 'site',
            'comment' => '应用站点信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(100)->notNull()->comment('站点名称'),
                'type' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('站点类型'),
                'title' => $this->string(100)->comment('站点标题'),
                'domain' => $this->char(100)->comment('站点域名'),
                'setting' => $this->text()->comment('站点设置'),
                'uuid' => $this->char(32)->comment('UUID'),
                'mark' => $this->string(255)->comment('备注'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 附件存储信息表
         * @todo create table `attachment`.
         */
        $this->tableBuilder([
            'name' => 'attachment',
            'comment' => '附件存储信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'path' => $this->char(255)->notNull()->comment('保存路径'),
                'original_name' => $this->string(255)->notNull()->comment('原始名称'),
                'size' => $this->bigInteger(20)->unsigned()->notNull()->defaultValue(0)->comment('文件大小（单位：字节）'),
                'channel' => $this->char(20)->comment('远端上传通道'),
                'isimage' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('是否图片'),
                'width' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('宽度'),
                'height' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('高度'),
                'download' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('浏览或者下载次数'),
                'ip' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('上传IP地址'),
                'auth_token' => $this->char(32)->notNull()->comment('校验Token'),
                'site_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('来源站点ID'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('附件状态'),
                'created_at' => $this->unixTimestamp(),
                'updated_at' => $this->unixTimestamp(),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ]
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%attachment}}');
        $this->dropTable('{{%site}}');
    }

}
